//
//  FansListViewController.m
//  SelfDriving
//
//  Created by apple on 16/2/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FansListViewController.h"
#import "MsgCell.h"
#import "BusinessService+FJ.h"
#import "BusinessService+User.h"
#import "FriendDetailsMainVC.h"
@interface FansListViewController ()<BaseTableDetelage>

{
    NSMutableArray *_contacts;//联系人模型
}
@property (weak, nonatomic) IBOutlet BaseTable *table;
@end

@implementation FansListViewController{
    NSMutableArray *data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    data = [[NSMutableArray alloc]init];
    _table.bdelegate = self;
    [self initData:0];
    
}
-(void)initData:(int)index{
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"type"];
    [[BusinessService shareInstance]myfollow:dic success:^(id result) {
        data = [_table result:result olddata:data index:index];
        data =  [ValidateHelper dataFrom:data];
        [_table reloadData];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
    
}



#pragma mark 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"计算分组数");
    return data.count;
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    KCContactGroup *group1 = data[section];
    return group1.contacts.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KCContactGroup *group=data[indexPath.section];
    LoginResponseModel *contact = group.contacts[indexPath.row];
    MsgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MsgCell"];
    cell.img.layer.cornerRadius = cell.img.frame.size.width/2;
    cell.img.clipsToBounds = YES;
    cell.name.text = contact.nickname;
    [ValidateHelper setImage:contact.headsmall img:cell.img istoux:YES];
    cell.job.text = [NSString stringWithFormat:@"%@岁", [ValidateHelper timeToAge:contact.age]];
    cell.content.text = contact.sign;
    cell.btn.layer.cornerRadius = 4;
    cell.btn.layer.borderWidth = 0.8;
    cell.btn.layer.borderColor = colorBG.CGColor;
    cell.btn.model = contact;
    if([contact.isfriend intValue]==3){
        cell.btn.titleLabel.text = @"已关注";
        [cell.btn setTitle:@"已关注" forState:UIControlStateNormal];
        cell.btn.enabled = YES;
    }
    return cell;
}
#pragma mark 关注
- (IBAction)guanzhuAction:(BaseButton *)sender {
    LoginResponseModel *contact = sender.model;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:contact.uid forKey:@"fid"];
    [dic setObject:@"0" forKey:@"type"];
    [[BusinessService shareInstance]follow:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"关注成功"];
        [_table Refresh];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}
#pragma mark 返回每组头标题名称
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    KCContactGroup *group=data[section];
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:group.name];
    return group.name;
}

#pragma mark 返回每组标题索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *indexs=[[NSMutableArray alloc]init];
    for(KCContactGroup *group in data){
        [indexs addObject:group.name];
    }
    return indexs;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KCContactGroup *group=data[indexPath.section];

    LoginResponseModel *contact = group.contacts[indexPath.row];

    DTModel *model = [[DTModel alloc]init];
    model.uid = contact.uid;
    
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = model;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}
- (IBAction)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
